package com.elvende.web.client.view.component;

import com.google.gwt.event.dom.client.MouseEvent;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.FlexTable;

public class ElvenTable extends FlexTable {
	  private HandlerManager manager = new HandlerManager(this); 
	  
	  int[] idx;
	  
	public ElvenTable(){
        super();
        addDomHandler(new MouseOverHandler() {
				public void onMouseOver(MouseOverEvent event) {					
				}
        }, MouseOverEvent.getType());
        
        sinkEvents(Event.ONMOUSEDOWN | Event.ONMOUSEUP | Event.ONMOUSEOVER |Event.ONMOUSEOUT);
        
	  } 
	  
	    public void onBrowserEvent(Event event) {
//            Element td = getEventTargetCell(event);                       
//            if (td == null) return;
//            Element tr = DOM.getParent(td);
//            Element table = DOM.getParent(tr);                                    
            switch (DOM.eventGetType(event)) {            
	            case Event.ONMOUSEOVER: {
	            	
	                // DOM.setStyleAttribute(tr, "backgroundColor", "#ffce00");
	            	// MouseOverEvent e = (MouseOverEvent) event;
	            	// idx = getCellForEvent((MouseOverEvent) event.getS);
	                break;
	            }
           }
	    }
	  	    	    
       public HandlerRegistration addMouseOverHandler(MouseOverHandler handler){
               return manager.addHandler(MouseOverEvent.getType(), handler);
       } 

       public HandlerRegistration addMouseOutHandler(MouseOutHandler handler){
           return manager.addHandler(MouseOutEvent.getType(), handler);
       }        
       
		@Override
		public void fireEvent(GwtEvent<?> event) {
		        manager.fireEvent(event);
		}		
		
		public int[] getCellForEvent(final MouseEvent<?> event) {
            final Element td = getEventTargetCell(Event.as(event.getNativeEvent()));
            if (td == null) {
                    return null;
            }
            final Element tr = DOM.getParent(td);
            final Element body = DOM.getParent(tr);

            final int r = DOM.getChildIndex(body, tr);
            final int c = DOM.getChildIndex(tr, td);

            final int[] idx = new int[] { r, c };

            return idx;
		} 				
	 }